/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverapplication;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static serverapplication.DesktopApplicationFunctionLibrary.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import pack.Course;
import pack.Question;

/**
 *
 * @author shuyi
 */
public class DesktopService implements Runnable {

    public DesktopService(Socket socket) {
        this.socket = socket;
        user = new UserProfile();
    }

    @Override
    public void run() {
        try {
            try {
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                try {
                    doService();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
            } finally {
                socket.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void doService() throws IOException, ClassNotFoundException {
        while (true) {
            //if(!in.hasNext())return;
            String command = (String) in.readObject();
            Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("Command: " + command + "\n");
            if (command.equalsIgnoreCase("QUIT")) {
                return;
            } else {
                executeCommand(command);
            }
        }
    }

    public void executeCommand(String command) throws IOException, ClassNotFoundException {

        //command login
        if (command.equalsIgnoreCase("LogIn")) {
            boolean success;
            String username = (String) in.readObject();
            String password = (String) in.readObject();
            success = checkLogIn(username, password, this.user);
            out.writeBoolean(success);
            if (success) {
                out.writeInt(this.user.user_id);
                out.writeObject(this.user.firstName);
                out.writeObject(this.user.lastName);
                out.writeObject(this.user.username);
                checkUserHasAnswerTable(this.user);
            }
            out.flush();
            System.out.println(success);
        } //command get course list
        else if (command.equalsIgnoreCase("GetCourseList")) {
            int user_id = in.readInt();
            ArrayList<Course> courseList = new ArrayList<Course>();
            try {
                courseList = getCourseList(user_id);
            } catch (SQLException ex) {
                Logger.getLogger(DesktopService.class.getName()).log(Level.SEVERE, null, ex);
            }
            out.writeObject(courseList);
            out.flush();
            Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(courseList.size() + "\n");
        } //command create course
        else if (command.equalsIgnoreCase("CreateCourse")) {
            int course_id = -1;
            int user_id = in.readInt();
            String title = (String) in.readObject();
            String body = (String) in.readObject();
            try {
                course_id = createCourse(user_id, title, body);
            } catch (SQLException ex) {
                Logger.getLogger(DesktopService.class.getName()).log(Level.SEVERE, null, ex);
            }
            out.writeInt(course_id);
            out.flush();
        } //command edit course
        else if (command.equalsIgnoreCase("EditCourse")) {
            int course_id = in.readInt();
            String title = (String) in.readObject();
            String body = (String) in.readObject();
            try {
                editCourse(course_id, title, body);
            } catch (SQLException ex) {
                Logger.getLogger(DesktopService.class.getName()).log(Level.SEVERE, null, ex);
            }
        } //command delete course
        else if (command.equalsIgnoreCase("RemoveCourse")) {
            int course_id = in.readInt();
            try {
                removeCourse(course_id);
            } catch (SQLException ex) {
                Logger.getLogger(DesktopService.class.getName()).log(Level.SEVERE, null, ex);
            }
        } // command add question
        else if (command.equalsIgnoreCase("CreateQuestion")) {
            String title = (String) in.readObject();
            String body = (String) in.readObject();
            int type = in.readInt();
            int course_id = in.readInt();
            int user_id = in.readInt();
            Question question = null;
            try {
                question = createQuestion(title, body, type, course_id, user_id);
            } catch (SQLException ex) {
                Logger.getLogger(DesktopService.class.getName()).log(Level.SEVERE, null, ex);
            }
            out.writeObject(question);
            out.flush();
        }
    }
    private Socket socket;
    private ObjectInputStream in;
    private ObjectOutputStream out;
    private UserProfile user;
}
